import axios from 'axios'
import loading from "@/components/Loading";

const service = axios.create()

// request 拦截器
service.interceptors.request.use(
  config => {
    const token = 'eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhYSIsInVzZXJJZCI6IjE0OTU2NzIzMTA4NTY2Nzk0MjQiLCJ1c2VyTmFtZSI6ImFhIiwibG9naW5OYW1lIjoieXFtXzJ3bW5mbncxIiwidG9rZW5JZCI6IjE3NzcxMzc5MzU0MTg2NTQ3MjAiLCJleHAiOjE3MTI1NDMwNzZ9.RAKIVCRX_4FP51vQ_6OAxqMVcSg20nqyVdDn4_aba7sfjHYpq4kzb76FDkUo7liEJWARUx7CM9dEszAeFc5jkhltBs-o_3SX6qxxsaQ6dxrX6avNO2xurOOcQwlHUq03xutFkkE8knXWWCjp5etvgzSXBwQWGMtu1XyX9u5xMag'
    const tempConfig = {
      ...config
    }
    if (token) {
      tempConfig.headers['tokenId'] = token
    }
    // loading.show()
    return tempConfig
  },
  error => {
    loading.hide()
    return Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
    loading.hide()
    return response.data.data
  },
  error => {
    loading.hide()
    return Promise.reject(error)
  }
)

export default service